package org.behrang.macbeans.tab;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import org.behrang.macbeans.Utils;
import org.netbeans.swing.tabcontrol.TabDisplayer;
import org.netbeans.swing.tabcontrol.plaf.AquaEditorTabDisplayerUI;
import org.netbeans.swing.tabcontrol.plaf.TabCellRenderer;
import org.netbeans.swing.tabcontrol.plaf.TabState;

/**
 *
 * @author behrangsa
 */
public class BMEditorTabDisplayerUI extends AquaEditorTabDisplayerUI {

    private static final int PREFERRED_HEIGHT = 23;
    
    public BMEditorTabDisplayerUI(TabDisplayer displayer) {
        super(displayer);        
    }
    
    @Override
    public void install() {
        super.install();
        scroll().setMinimumXposition(9);
    }
    
    @Override
    protected TabCellRenderer createDefaultRenderer() {
        return new BMEditorTabCellRenderer();        
    }    
    
    public static ComponentUI createUI(JComponent c) {
        return new BMEditorTabDisplayerUI((TabDisplayer) c);
    }

    @Override
    protected void paintAfterTabs(Graphics g) {
        
        int rightLineStart = getTabsAreaWidth();
        int rightLineEnd = displayer.getWidth();        
        
        if (displayer.getModel().size() > 0 && !scroll().isLastTabClipped()) {            
            int idx = scroll().getLastVisibleTab(displayer.getWidth());
            rightLineStart = scroll().getX(idx) + scroll().getW(idx);
        } else if (displayer.getModel().size() == 0) {
            rightLineStart = 6;
        }
        
        if (scroll().getOffset() >= 0) {
            //fill the left edge notch
            g.drawLine(6, 0, 11, 0);
        }
                
        
        // Utils.prints("Disp.Height", displayer.getHeight());

        final Color old = g.getColor();       
        
        g.setColor(new Color(174, 174, 174));
        g.drawLine(rightLineStart, 0, rightLineEnd, 0);
        g.setColor(new Color(64, 64, 64));
        g.drawLine(rightLineStart, 1, rightLineEnd, 1);
        g.drawLine(rightLineStart, 22, rightLineEnd, 22);
        g.setColor(new Color(153, 153, 153));
        g.drawLine(rightLineStart, 21, rightLineEnd, 21);
        
        g.setColor(old);
    }

    /**
     * Overridden to adjust the visible height of the tab displayer area.
     * 
     * @param c
     * @return
     */
    @Override
    public Dimension getPreferredSize(JComponent c) {
        Dimension ps = super.getPreferredSize(c);
        ps.height = Math.max(PREFERRED_HEIGHT, ps.height);        
        return ps;
    }

    /**
     * Overridden to adjust the height of the rendering bound of tabs.
     * 
     * @param tabIndex
     * @param bounds
     * @return
     */
    @Override
    public Rectangle getTabRect(int tabIndex, Rectangle bounds) {
        Rectangle tabRect = super.getTabRect(tabIndex, bounds);
        tabRect.height = Math.max(PREFERRED_HEIGHT, tabRect.height);
        return tabRect;
    }

    @Override
    protected int createRepaintPolicy() {
        return super.createRepaintPolicy() | TabState.REPAINT_ON_SELECTION_CHANGE;
    }

    
    
}